﻿using GraphDrawing2.Canvas;
using GraphDrawing2.V2.Concrete.States;
using GraphDrawing2.V2.Graph.Model;

namespace GraphDrawing2.V2.Concrete.Model
{
    public class CanvasListener : IListener
    {
        readonly Model2 model;
        readonly DefaultState defaultState;
        IState state;

        public CanvasListener(CanvasModel canvas, ElementsModel graph, ICanvasView view)
        {
            model = new Model2(canvas, graph, view);
            state = defaultState = new DefaultState(model);
        }

        public void OnMouseWheel(int value)
        {
            if (value == 0)
                return;
            var viewPort = model.CanvasModel.ViewPort;
            if (value > 0)
                viewPort.ZoomIn();
            else
                viewPort.ZoomOut();
        }

        public void OnMouseMove()
        {
            model.UpdateHitTest();
            state.ProcessMouseMove();
        }

        public void OnKeyPressed()
        {
            state.ProcessKeyPressed();
        }

        public void OnMouseUp()
        {
            model.UpdateHitTest();
            state.ProcessMouseUp();
            state = defaultState;
        }

        public void OnMouseDown()
        {
            model.UpdateHitTest();
            var visitor = new CreateHitTestStateVisitor(model);
            state = model.HitTest.ProcessBy(visitor);
        }

        public void OnDragDrop()
        {
        }

        public void OnDragLeave()
        {
        }

        public void OnDragEnter()
        {
        }
    }
}